package com.zhao.dota.config;

import com.zhao.dota.security.store.EhcacheTokenStore;
import com.zhao.dota.security.store.InMemoryTokenStore;
import com.zhao.dota.security.store.TokenStore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.Resource;

@Configuration
public class TokenStoreConfig {

    @Resource
    private CacheManager cacheManager;

    @Bean
    @Primary
    @ConditionalOnProperty(name = "use-redis", havingValue = "false", matchIfMissing = true)
    public TokenStore ehcacheTokenStore() {
        return new EhcacheTokenStore(cacheManager);
    }

    @ConditionalOnMissingBean(TokenStore.class)
    @Bean
    public TokenStore inMemoryTokenStore() {
        return new InMemoryTokenStore();
    }

}
